\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\f0\b0\i0\ulnone\fs28\fc1\cf1 Q: Can I integrate C++ code into my InterfaceBuilder/Objective-C application? How?\
\
A: Yes, in Release 2 and following releases you can, and it's pretty easy (once you know how)! The procedure breaks down into three categories of things that you must do: compiling, ProjectBuilder and getting the two languages to talk to each other.\
\
\b Compiling
\b0 \
First, you must use the C++ compiler for
\i all
\i0 of your source files—including the Objective-C sources. To do this, add the following line to your Makefile.preamble:\
Now that you are using the C++ compiler, you have to notify the compiler when/if your header files contain non-C++ code. For Objective-C header files, encapsulate your
\pard\tx620\tx1240\tx1860\tx2480\tx3100\tx3720\tx4340\tx4980\tx5600\tx6220\f0\fc1\cf1 The C++ "linkage" directive serves two purposes (when importing interface files that contain straight ANSI-C/Objective-C code). It:\
\
\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fi-980\li980\fc0\cf0 • a
\fc1\cf1 llows you to link with libraries that have not been compiled with the C++ compiler. Since libraries in NEXTSTEP are compiled with the Objective-C compiler (cc, not cc++), you must use the C++ linkage directive when importing interface files that represent NeXT libraries (or any library that is not compiled with cc++). \
\
\fc0\cf0 •
\fc1\cf1 tells the compiler to ignore C++ keywords that result in syntax errors when importing ANSI-C or Objective-C interface files. The linkage directive essentially tells the C++ compiler to treat keywords (such as the method names "new", "delete", etc.) as normal identifiers.\
Since the nib files generated by InterfaceBuilder are based the AppKit, and it generates source templates in Objective-C, we must envision our program such that Objective-C and nib files are the foundation of our program, and the C++ code is a supporting library.\
\
Now that we can compile, we need to get an Objective-C object and a C++ object to pass messages to one another. Suppose that we have two created objects—a C++ object and an Objective-C object. This is how you
\b
\b0 would refer to the C++ object and tell it to "do something":\
C++ objects are implemented as regular C structures, so to access public instance variables, or public methods of a C++ object, you dereference the object with the -> syntax as you would a structure member. And this is how you would refer to an Objective-C object from C++:\
\
\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f2\fs24\fi-980\li980\fc1\cf1 id objectiveObj;\
Basically, in either case you use the language constructs of the object to which you are referring, and embed them in the source file of the other language.\
\
\b Example
\b0 \
There is an example located in /NextDeveloper/Examples/AppKit/CalculatorLab++ which illustrates the integration of InterfaceBuilder nib files, Objective-C source code, and C++ source code into one program.\
\
QA584\
\
\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc0\cf0 Valid for 2.0,